package Controller.Sign;

import Model.SignModel.DeviceLr;
import Model.User;
import Service.DeviceLrService;
import Service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.permission.ConstantsUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/DeviceLr")
public class DeviceLrController {

    @Autowired
    private DeviceLrService deviceLrService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getDeviceLrList")
    @ResponseBody
    public PageInfo getDeviceList(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "limit", defaultValue = "10") Integer limit, HttpServletRequest request, DeviceLr deviceLr){
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if(Role_id.equals("2")){
            String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
            System.out.println("找到机构代码："+settleCode);
            deviceLr.setOrganization_id(settleCode);
            PageHelper.startPage(pn, limit);
            List<DeviceLr> deviceLrList = deviceLrService.getDeviceLrList(deviceLr);
            System.out.println("[获取到老人设备数据：]"+deviceLrList);
            PageInfo page = new PageInfo(deviceLrList);
            return page;
        }else{
            PageHelper.startPage(pn, limit);
            List<DeviceLr> deviceLrList = deviceLrService.getDeviceLrList(deviceLr);
            System.out.println("[获取到老人设备数据：]"+deviceLrList);
            PageInfo page = new PageInfo(deviceLrList);
            return page;
        }
    }

    @RequestMapping(value = "/updateLrDeviceState")
    @ResponseBody
    public int updateLrDeviceState(DeviceLr deviceLr){
        int status = 0;
        try{
            deviceLrService.updateLrDeviceState(deviceLr);
            System.out.println("[更新老人设备状态完成~]");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/deleteLrDevice")
    @ResponseBody
    public int deleteLrDevice(int id){
        int status = 0;
        try {
            DeviceLr deviceLr = new DeviceLr();
            deviceLr.setId(id);
            List<DeviceLr> list = deviceLrService.getDeviceLrList(deviceLr);
            int state = list.get(0).getState();
            System.out.println("获取到当前设备的状态是："+state);
            if(state==0){
                System.out.println("设备已使用，不能删除！");
                status = 2;
            }else {
                deviceLrService.deleteLrDevice(id);
                System.out.println("删除老人设备成功~");
            }
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/updateLrDevice")
    @ResponseBody
    public int updateLrDevice(DeviceLr deviceLr){
        int status = 0;
        try {
            deviceLrService.updateLrDevice(deviceLr);
            System.out.println("更新老人设备完成~");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/addLrDevice")
    @ResponseBody
    public int addLrDevice(DeviceLr deviceLr,HttpServletRequest request){
        int status = 0;
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
        System.out.println("当前机构ID为："+settleCode);
        deviceLr.setOrganization_id(settleCode);
        String organization = user.getUser_name();
        System.out.println("当前机构为："+settleCode);
        deviceLr.setOrganization(organization);
        try{
            deviceLrService.addLrDevice(deviceLr);
            System.out.println("添加老人设备成功~");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/getLrDevice")
    @ResponseBody
    public List<DeviceLr> getLrDevice(HttpServletRequest request, DeviceLr deviceLr){
        deviceLr.setState(-1);
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if (Role_id.equals("2")){
            String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
            System.out.println("找到机构代码："+settleCode);
            deviceLr.setOrganization_id(settleCode);
            List<DeviceLr> list = deviceLrService.getLrDevice(deviceLr);
            System.out.println("获取到老人设备一列："+list);
            return list;
        }else{
            List<DeviceLr> list = deviceLrService.getLrDevice(deviceLr);
            System.out.println("获取到老人设备一列："+list);
            return list;
        }
    }
}
